package org.linlinjava.litemall.admin.controller.giftcards;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.dto.CardDto;
import org.linlinjava.litemall.admin.util.AdminResponseEnum;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallGiftCardUser;
import org.linlinjava.litemall.db.service.LitemallGiftCardUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理礼品卡用户控制器
 *
 * @author yliyun
 * @description：礼物卡用户购买情况
 * @date 2022/09/14
 */

@RestController
@RequestMapping("admin/giftcarduser")
public class AdminGiftCardUserController {
    /**
     * litemall礼品卡用户服务
     */
    @Autowired
    private LitemallGiftCardUserService litemallGiftCardUserService;

    /**
     * 列表
     *
     * @param giftCardId 礼品卡id
     * @param userName   用户名
     * @param page       页面
     * @param limit      限制
     * @param sort       排序
     * @param order      订单
     * @return {@link Object}
     */
    @GetMapping("/list")
    @LogAnno
    public Object list(Integer giftCardId, String userName,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order){
        List<LitemallGiftCardUser> litemallGiftCardUsers = litemallGiftCardUserService.querySelective(giftCardId, userName, page, limit, sort,
                order);
        return ResponseUtil.okList(litemallGiftCardUsers);
    }

    /**
     * 绑定
     *
     * @param dto dto
     * @return {@link Object}
     */
    @PostMapping("/bind")
    @LogAnno
    public Object bind(@Valid @RequestBody CardDto dto){
        LitemallGiftCardUser giftCardUser = new LitemallGiftCardUser();
        giftCardUser.setId(dto.getId());
        giftCardUser.setEntityCardCode(dto.getEntityCardCode());
        litemallGiftCardUserService.updateById(giftCardUser);
        return ResponseUtil.ok();
    }

    /**
     * 解开
     *
     * @param id id
     * @return {@link Object}
     */
    @PutMapping("/unbind")
    @LogAnno
    public Object unbind(@NotNull Integer id){
        LitemallGiftCardUser giftCardUser = litemallGiftCardUserService.findById(id);
        if(giftCardUser == null){
            return ResponseUtil.fail(AdminResponseEnum.GIFTCARD_NOT_EXIST);
        }
        giftCardUser.setEntityCardCode(null);
        litemallGiftCardUserService.unbind(giftCardUser);
        return ResponseUtil.ok();
    }
}
